import { Group, Material, Mesh, MeshStandardMaterial, PlaneGeometry } from "three";
import { OBJLoader } from "three/examples/jsm/loaders/OBJLoader";
import { cameraFrameColorTexture, cameraFrameDisplacementTexture, cameraFrameNormalTexture, cameraFrameRoughnessTexture, pictrueTexture, tipsTexture } from './tTextures';

// 模型加载器
const objLoader: OBJLoader = new OBJLoader()

// 材质加载器(通过建模软件导出的材质会有一定问题，threejs不兼容)
// const mtlLoader: MTLLoader = new MTLLoader()
// const meterial: Promise<MTLLoader.MaterialCreator> = mtlLoader.loadAsync("/camera-frame/frame.mtl")
// export const framePromise: Promise<Group> = meterial.then(materialCreator=>{
//     return  objLoader
//             .setMaterials(materialCreator)
//             .loadAsync("/camera-frame/frame.obj")
// })
// .catch(err=>{
//     return err
// })

// 通过自己构造threjs的材质
export const frameMaterial: MeshStandardMaterial = new MeshStandardMaterial({
    map: cameraFrameColorTexture,
    roughnessMap: cameraFrameRoughnessTexture, 
    // 移位贴图会对顶点实际位移有影响
    // displacementMap: cameraFrameDisplacementTexture,
    // 对顶点实际位移无影响
    bumpMap: cameraFrameDisplacementTexture,
    normalMap: cameraFrameNormalTexture
})

// 标签
export const tips: Mesh = new Mesh(
    new PlaneGeometry(16,9),
    new MeshStandardMaterial({
        map: tipsTexture,
    })
)
tips.position.set(0,-30,0)
tips.rotation.y = Math.PI / 180* 90

export const framePromise: Promise<Group> = objLoader.loadAsync("/camera-frame/frame.obj")
                                            .then(group=>{
                                                group.position.y = 100
                                                group.position.z = -72
                                                group.rotation.y = (Math.PI / 180 * -90)

                                                group.add(tips)
                                                group.scale.set(2,2,2)
                                                
                                                let frameModel = group.getObjectByName("立方体_立方体.001") as Mesh;
                                                // 产生阴影
                                                frameModel.castShadow = true;
                                                (frameModel.material as Material).dispose()
                                                frameModel.material = frameMaterial
                                                console.log(group)
                                                return group;
                                            })
                                            .catch(err=>err)